package com.common.frame.widget

import androidx.core.view.isVisible
import com.common.frame.R
import com.common.frame.base.BaseDialogFragment
import com.common.frame.base.setHtmlText
import com.common.frame.databinding.DialogCommomTipsBinding
import com.common.frame.extension.dpToPx
import com.common.frame.extension.setSingleClick

class CommonTipsDialog : BaseDialogFragment<DialogCommomTipsBinding>() {

    var title = "提示"
    var content = ""
    var leftText = "取消"
    var rightText = "确定"
    var showLeftBtn = true
    var showRightBtn = true
    var leftCallback: (() -> Unit)? = null
    var rightCallback: (() -> Unit)? = null
    var onDismiss: (() -> Unit)? = null
    var canCancelable = true

    override fun getLayoutId(): Int = R.layout.dialog_commom_tips

    override fun initView() {
        binding.tvTitle.text = title
        binding.tvContent.isVisible = content.isNotBlank()
        binding.tvContent.setHtmlText(content)
        binding.btnLeft.text = leftText
        binding.btnRight.text = rightText
        binding.btnLeft.isVisible = showLeftBtn
        binding.btnRight.isVisible = showRightBtn
    }

    override fun cancelable(): Boolean = canCancelable

    override fun initListener() {
        binding.btnLeft.setSingleClick {
            leftCallback?.invoke()
            dismissAllowingStateLoss()
        }
        binding.btnRight.setSingleClick {
            rightCallback?.invoke()
            dismissAllowingStateLoss()
        }
    }

    fun setTitle(title: String): CommonTipsDialog {
        this.title = title
        return this
    }

    fun setContent(content: String): CommonTipsDialog {
        this.content = content
        return this
    }

    fun setLeftText(leftText: String): CommonTipsDialog {
        this.leftText = leftText
        return this
    }

    fun setRightText(rightText: String): CommonTipsDialog {
        this.rightText = rightText
        return this
    }

    fun setShowLeftBtn(showLeftBtn: Boolean): CommonTipsDialog {
        this.showLeftBtn = showLeftBtn
        return this
    }

    fun setShowRightBtn(showRightBtn: Boolean): CommonTipsDialog {
        this.showRightBtn = showRightBtn
        return this
    }

    fun setLeftCallback(leftCallback: () -> Unit): CommonTipsDialog {
        this.leftCallback = leftCallback
        return this
    }

    fun setRightCallback(rightCallback: () -> Unit): CommonTipsDialog {
        this.rightCallback = rightCallback
        return this
    }

    fun setDismiss(onDismiss: () -> Unit): CommonTipsDialog {
        this.onDismiss = onDismiss
        return this
    }

    fun setCanCancelable(cancelable: Boolean): CommonTipsDialog {
        canCancelable = cancelable
        return this
    }

    override fun getMargin(): Int = 60.dpToPx()

}